package org.zjx.command.login;

import org.zjx.command.Command;
import org.zjx.command.CompositeCommand;
import org.zjx.command.CompositeCommandBuilder;
import org.zjx.command.basic.ClickCommand;
import org.zjx.command.basic.DelayCommand;
import org.zjx.command.basic.MoveCommand;
import org.zjx.command.basic.ScreenshotCommand;
import org.zjx.command.composite.FindImageCommand;
import org.zjx.command.composite.IdentifyTessCommand;
import org.zjx.core.IWindow;
import org.zjx.state.StateManager;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;

public class LoginRoleCommand extends CompositeCommand {
    public LoginRoleCommand(IWindow window, StateManager stateManager) {
        super(window, stateManager);
    }

    @Override
    protected void initializeSubCommands() {
        String id = stateManager.getAccountState().getPlayerId();
        if (id == null) {
            createRole();
        } else {
            chooseRole(id);
        }
    }

    private void chooseRole(String id) {
        add(new Command(targetWindow, false, stateManager) {
            @Override
            protected void doExecute() {
                List<Rectangle> rectangles = LoginCommandConfig.Buttons.ROLE_RECT;

                List<Color> checkedColor = Collections.singletonList(new Color(118, 255, 141));
                List<Color> unCheckedColor = Collections.singletonList(new Color(255, 242, 167));

                for (int i = 0; i < rectangles.size(); i++) {
                    Rectangle rectangle = rectangles.get(i);
                    ScreenshotCommand screenshotCommand = new ScreenshotCommand(targetWindow);
                    screenshotCommand.execute();
                    BufferedImage screenshot = screenshotCommand.getScreenshot();

                    if (haveColor(screenshot, rectangles.get(i),unCheckedColor)){
                        clickRectangle(rectangle);
                        i--;
                        continue;
                    }

                    if (haveColor(screenshot, rectangles.get(i),checkedColor) && beEquals(screenshot)){
                        Rectangle interButton = LoginCommandConfig.Buttons.ROLE_INTER_BUTTON;
                        clickRectangle(interButton);
                        return;
                    }

                }
                throw new IllegalArgumentException("角色" + stateManager.getAccountState().getPlayerId() + "不存在!!!");
            }

            private void clickRectangle(Rectangle interButton) {
                MoveCommand moveCommand = new MoveCommand(targetWindow,(int) interButton.getCenterX(),(int)interButton.getCenterY(),stateManager);
                moveCommand.execute();
                DelayCommand delayCommand = new DelayCommand(targetWindow,50);
                delayCommand.execute();
                ClickCommand clickCommand = new ClickCommand(targetWindow,1,1);
                clickCommand.execute();
                delayCommand = new DelayCommand(targetWindow,500);
                delayCommand.execute();
            }
        });

    }

    private boolean haveColor(BufferedImage image,Rectangle rectangle, List<Color> colors) {
        BufferedImage bufferedImage = ImageUtil.cutImage(image,rectangle);
        return ImageUtil.haveColor(bufferedImage, colors);
    }

    private boolean beEquals(BufferedImage screenshot) {
        Rectangle idRectangle = LoginCommandConfig.Buttons.ROLE_ATTRIBUTE_RECT.get(1);
        IdentifyTessCommand tessCommand = new IdentifyTessCommand(targetWindow, stateManager, screenshot, idRectangle);
        tessCommand.execute();

        String tessId = tessCommand.getResult();
        return stateManager.getAccountState().getPlayerId().equals(tessId);
    }

    private void createRole() {
        CompositeCommandBuilder builder = new CompositeCommandBuilder(targetWindow, stateManager);

        CompositeCommand serverFlow = builder
                .find(LoginCommandConfig.Images.ROLE_CREATE_IMAGE, LoginCommandConfig.Buttons.ROLE_CREATE_BUTTON, "createButtonPoint")
                .ifFound("createButtonPoint", (b, context) -> {
                    Point point = (Point) context.get("createButtonPoint");
                    // 使用熟悉的构建器语法添加系列指令
                    b.move(point.x, point.y)
                            .delay(30)
                            .click()
                            .delay(1000);
                })
                .build();

        add(new MoveCommand(targetWindow, 30, 30, stateManager));
        add(serverFlow);
    }

    public static boolean validate(IWindow window, BufferedImage screenshot) {
        FindImageCommand findImageCommand = new FindImageCommand(window, screenshot, LoginCommandConfig.Images.ROLE_CREATE_IMAGE, LoginCommandConfig.Buttons.ROLE_CREATE_BUTTON);
        findImageCommand.execute();
        return findImageCommand.findImage();
    }
}